package com.utils.conmponentbase.base.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.utils.conmponentbase.base.inner.IView;
import com.utils.conmponentbase.base.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * MvvmDemo com.utils.conmponentbase.base
 * 2020-01-04 09:32
 */
public abstract class BaseFragment<Binding extends ViewDataBinding, VM extends BaseViewModel> extends Fragment implements IView {
    protected Activity mContext;
    protected Binding mDataBinding;
    protected VM mViewModel;
    //是否第一次加载
    private boolean isFirstLoad = true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity();
        if (getLayoutId() <= 0 ) throw new NullPointerException("LayoutId 不能为空，先设置布局文件");
        mDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        mDataBinding.setLifecycleOwner(this);
        if (null == mViewModel) {
            Class modelClass;
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[1];
            } else {
                //如果没有指定泛型参数，则默认使用BaseViewModel
                modelClass = BaseViewModel.class;
            }
            mViewModel = (VM) ViewModelProviders.of(getActivity()).get(modelClass);
        }
        //View view = inflater.inflate(getLayoutId(), container, false);
        initView(mDataBinding.getRoot());
        return mDataBinding.getRoot();
    }

    /**
     * 初始化视图
     *
     * @param view view
     */
    protected void initView(View view) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    public VM getViewModel() {
        return mViewModel;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad) {
            initData();
            isFirstLoad = false;
        }
    }

    /**
     * 无参数传递跳转到Activity
     * 无跳转动画
     */
    public void gotoActivity(String path) {
        ARouter.getInstance().build(path).navigation();
    }

    /**
     * 带参数传递跳转到Activity
     * 无跳转动画
     */
    public Postcard withValueActivity(String path) {
        Postcard aRouter = ARouter.getInstance().build(path);
        return aRouter;
    }
}
